#!/usr/bin/python2.7

import os


class Tree:
	
	def __init__(self, value):
		self.value = value
		self.children = [] 

	def addChild(self, value):
		newTree = Tree(value)
		self.children.append(newTree)
		return newTree

	def find(self, value):
		if self.value == value:
			return self
		else:
			for subtree in self.children:
				tree = subtree.find(value)
				if not tree is None:
					return tree
			return None

	def recToString(self, level):
		result = "|  "*level + self.value + "\n"
		if len(self.children) > 0:
			for child in self.children:
				result += child.recToString(level + 1)
		return result

	def toString(self):
		return self.recToString(0)	




if __name__=="__main__":

	def createBranch(tree, path):
		dirs = path[1:].split("/")
		for dira in dirs:
			new_tree = tree.find(dira)
			if new_tree is None:
				new_tree = tree.addChild(dira)
			tree = new_tree
				

	t = Tree("Coucou")

	t1 = t.addChild("child1")
	t2 = t.addChild("child2")
	t1.addChild("child1.1")
	t1.addChild("child1.2")
	t1.addChild("child1.3")

	t2.addChild("child2.1")
	t2.addChild("child2.2")

	print t.toString()

	#subtree = t.find("child3")

	#print subtree.toString()

	filetree = Tree("*")
	for root, dirs, files in os.walk("/home/clems/projects"):
		for filea in files:
			createBranch(filetree, root + "/"+ filea)
	print filetree.toString()


